package cn.wolfcode.wolf.article.service.impl;

import cn.wolfcode.wolf.article.domain.Destination;
import cn.wolfcode.wolf.article.domain.Region;
import cn.wolfcode.wolf.article.mapper.DestinationMapper;
import cn.wolfcode.wolf.article.qo.DestinationQuery;
import cn.wolfcode.wolf.article.service.DestinationService;
import cn.wolfcode.wolf.article.service.RegionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper, Destination> implements DestinationService {
    @Autowired
    private RegionService regionService;
    @Autowired
    private ThreadPoolExecutor businessThreadPoolExecutor;

    @Override
    public List<Destination> getDestinationByRegionId(Long id) {
        Region region = regionService.getById(id);
        if (region == null) {
            return Collections.emptyList();
        }
        List<Long> refIds = region.parseRefIds();
        if (refIds.isEmpty()) {
            return Collections.emptyList();
        }
        return super.listByIds(refIds);
    }

    @Override
    public Page<Destination> pageList(DestinationQuery destinationQuery) {
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull(destinationQuery.getParentId() == null, "parent_id");
        queryWrapper.eq(destinationQuery.getParentId() != null, "parent_id", destinationQuery.getParentId());
        queryWrapper.like(!StringUtils.isEmpty(destinationQuery.getKeyword()), "name", destinationQuery.getKeyword());
        return super.page(new Page<>(destinationQuery.getCurrent(), destinationQuery.getSize()), queryWrapper);
    }

    @Override
    public List<Destination> findToasts(Long destinationId) {
        List<Destination> destinations = new ArrayList<>();
        while (destinationId != null) {
            Destination destination = super.getById(destinationId);
            if (destination == null) {
                break;
            }
            destinations.add(destination);
            destinationId = destination.getParentId();
        }
        Collections.reverse(destinations);
        return destinations;
    }


    //获取hotList,mybatis-plus方法

    @Override
    public List<Destination> findDestsByRid(Long rid) {
        List<Destination> destinations = new ArrayList<>();
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        //查询国内数据
        if (rid < 0) {
            destinations = list(queryWrapper.eq("parent_id", 1));
        } else {
            //查询国外数据
            Region region = regionService.getById(rid);
            if (region == null) {
                return Collections.emptyList();
            }
            destinations = super.listByIds(region.parseRefIds());
        }
        //创建计数器统计线程数量
        CountDownLatch countDownLatch = new CountDownLatch(destinations.size());
        //查询所有目的地的下一级目的地
        for (Destination destination : destinations){
//            //清空条件
//            queryWrapper.clear();
//            destination.setChildren(list(queryWrapper.eq("parent_id", destination.getId()).last("limit 10")));
              businessThreadPoolExecutor.execute(() ->{
                  destination.setChildren(list(new QueryWrapper<Destination>().eq("parent_id", destination.getId()).last("limit 10")));
              //线程数量减一
              countDownLatch.countDown();
              });
        }
        try {
            //等待所有线程执行完毕
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return destinations;

    }

    //  获取hotList,mybatis方法

//    @Override
//    public List<Destination> findDestsByRid(Long rid) {
//        List<Destination> destinations = new ArrayList<>();
//        //查询国内数据
//        if (rid < 0) {
//            destinations= this.getBaseMapper().selectHotListByRid(rid, Collections.emptyList());
//        }else {
//            //查询国外数据
//            Region region = regionService.getById(rid);
//            if (region == null) {
//                return Collections.emptyList();
//            }
//            destinations= this.getBaseMapper().selectHotListByRid(rid, region.parseRefIds());
//        }
//        for (Destination destination : destinations) {
//            List<Destination> children = destination.getChildren();
//            if (children != null && children.size() > 10) {
//                destination.setChildren(children.subList(0, 10));
//            }
//        }
//        return destinations;
//    }

}
